﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApp.BLL.Base;
using WebApp.DAL;
using WebApp.Models;
using System.Web;
using WebApp.Common;

namespace WebApp.BLL.Manage
{
    public class S_user_logService : BaseService<S_user_log>
    {
        #region DAL

        #endregion

        #region 构造

        public S_user_logService() : base(new S_user_logRepository()) { }

        #endregion

        #region userLog

        public void addUserLog(enmOprtType flag, int user_id, string username, string comments, string devicetype = "")
        {
            S_user_log log = new S_user_log();
            log.User_id = user_id;
            log.Username = username;
            log.Oprttype = (int)flag;
            log.Oprttime = DateTime.Now;
            log.Comments = comments;
            log.Ipaddress = WebUtils.getIPAddress(HttpContext.Current);
            log.Devicetype = devicetype;

            this.CurrentRepository.Add(log);
        }

        #endregion

        #region PageList

        public List<S_user_log> PageList(int pageindex, int pagesize, int type, string fromsearchdate, string tosearchdate, string searchkey, string sort, string order, out int rowscount)
        {
            var T = this.CurrentRepository.Entities;
            if (string.IsNullOrEmpty(sort)) sort = "Oprttime";
            T = base.SortList(T, sort, order == "desc" ? true : false);

            //查询条件
            if (!string.IsNullOrEmpty(searchkey))
            {
                T = T.Where(p => p.Username.Contains(searchkey) || p.Ipaddress.Contains(searchkey));
            }
            if (type > 0)
            {
                T = T.Where(p => p.Oprttype == type);
            }

            if (!string.IsNullOrEmpty(fromsearchdate))
            {
                DateTime fdate = Convert.ToDateTime(fromsearchdate);
                T = T.Where(p => p.Oprttime >= fdate);
            }
            if (!string.IsNullOrEmpty(tosearchdate))
            {
                DateTime tdate = Convert.ToDateTime(tosearchdate);
                T = T.Where(p => p.Oprttime < tdate.AddDays(1));
            }
            //返回记录数
            rowscount = T.Count();
            //返回分页结果
            return this.CurrentRepository.PageList(T, pageindex, pagesize).ToList();
        }

        public List<S_user_log> PageList(int pageindex, int pagesize, int type, int user_id, string fromsearchdate, string tosearchdate, string searchkey, string sort, string order, out int rowscount)
        {
            var T = this.CurrentRepository.Entities;

            if (user_id > 0)
            {
                T = T.Where(p => p.User_id == user_id);
            }

            if (string.IsNullOrEmpty(sort)) sort = "Oprttime";
            T = base.SortList(T, sort, order == "asc" ? true : false);

            //查询条件
            if (!string.IsNullOrEmpty(searchkey))
            {
                T = T.Where(p => p.Comments.Contains(searchkey) || p.Ipaddress.Contains(searchkey));
            }
            if (type > 0)
            {
                T = T.Where(p => p.Oprttype == type);
            }
            if (!string.IsNullOrEmpty(fromsearchdate))
            {
                DateTime fdate = Convert.ToDateTime(fromsearchdate);
                T = T.Where(p => p.Oprttime >= fdate);
            }
            if (!string.IsNullOrEmpty(tosearchdate))
            {
                DateTime tdate = Convert.ToDateTime(tosearchdate);
                T = T.Where(p => p.Oprttime < tdate.AddDays(1));
            }

            //返回记录数
            rowscount = T.Count();
            //返回分页结果
            return this.CurrentRepository.PageList(T, pageindex, pagesize).ToList();
        }

        #endregion

        #region 删除

        public bool DeleteByIds(string ids)
        {
            foreach (string id in ids.Split(','))
            {
                if (string.IsNullOrEmpty(id)) continue;

                //物理删除
                var item = this.CurrentRepository.Find(Convert.ToInt32(id));
                this.CurrentRepository.Delete(item, false);
            }

            this.CurrentRepository.Save();

            return true;
        }

        #endregion
    }
}
